import { createRouter, createWebHistory } from "vue-router";
import homeRouter from "../views/home/router";
console.log(...homeRouter);
//打印数组中的六个对象

const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/index",
    name: "Index",
    component: () => import("../views/Index.vue"),
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("../views/home/Home.vue"),
      },
      {
        path: "/personal",
        name: "personal",
        component: () => import("../views/personalCenter/Personal.vue"),

        beforeEnter(to, from) {
          console.log(to);
          if (!localStorage.getItem("token")) {
            return "/login";
          }
        },
        //因为组合api中组件级守卫没有 beforeRouteEnter，所以我们选择用单个路由规则的守卫，没有参数next了 不会拦截了 会自动放行，所以我们在这里只需要判断没有登陆的情况 跳转到login页面 用return跳转
      },
    ],
  },
  ...homeRouter,
  //把这个数组展开在这个位置
  {
    path: "/login",
    name: "Login",
    component: () => import("../views/personalCenter/Login.vue"),
    beforeEnter(to, from) {
      if (localStorage.getItem("token")) {
        return "/personal";
      }
    },
    //守卫登陆页面 如果已经登陆了 就不能访问 而是跳转到个人中心页面
  },
  {
    path: "/resign",
    name: "resign",
    component: () => import("../views/personalCenter/Resign.vue"),
  },
];

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes,
});

export default router;
